#! /bin/bash

# 获取环境变量
PLAY_HOME=/opt/playground

# 配置node.properties文件
function configureNodeEnv()
{
 trinoEnvUrl=$1
 node=`hostname`
 
cat >> $trinoEnvUrl <<EOF
node.environment=production
node.id=$node
node.data-dir=/var/trino/data
EOF
}

# 配置jvm.config文件
function configureJvm()
{
 trinoJvmUrl=$1
 
cat >> $trinoJvmUrl <<EOF
-server
-Xmx16G
-XX:+UseG1GC
-XX:G1HeapRegionSize=32M
-XX:+UseGCOverheadLimit
-XX:+ExplicitGCInvokesConcurrent
-XX:+HeapDumpOnOutOfMemoryError
-XX:+ExitOnOutOfMemoryError
EOF
}

# 配置主节点
function configureTrinoCoordinator()
{
 trinoSiteUrl=$1
 node=$2
 
 cat >> $trinoSiteUrl <<EOF
coordinator=true
node-scheduler.include-coordinator=false
http-server.http.port=8080
query.max-memory=50GB
query.max-memory-per-node=1GB
discovery-server.enabled=true
discovery.uri=http://$node:8080
EOF
}

# 配置从节点
function configureTrinoWorker()
{
 trinoSiteUrl=$1
 node=$2
 
 cat >> $trinoSiteUrl <<EOF
coordinator=false
http-server.http.port=8080
query.max-memory=50GB
query.max-memory-per-node=1GB
discovery.uri=http://$node:8080
EOF
}

# 配置日志级别
function configureLogLevel()
{
 trinoLogUrl=$1
 echo "io.trino=INFO" > $trinoLogUrl
}

# 配置Jmx catalog
function configureJmxCatalog()
{
 trinoCatalogUrl=$1
 echo "connector.name=jmx" > $trinoCatalogUrl
}

# 配置Hive Catalog
function configureHiveCatalog()
{
 trinoCatalogUrl=$1
 hiveNode=$2
 echo "connector.name=hive" >> $trinoCatalogUrl
 echo "hive.metastore.uri=thrift://$hiveNode:9083" >> $trinoCatalogUrl
}

function installTrino()
{
 #1.在frames.txt中查看是否需要安装trino
 trinoInfo=`egrep "trino" $PLAY_HOME/frames.txt`
 hiveInfo=`egrep "hive" $PLAY_HOME/frames.txt`

 trino=`echo $trinoInfo | cut -d " " -f1`
 isInstall=`echo $trinoInfo | cut -d " " -f2`
 installNode=`echo $trinoInfo | cut -d " " -f3`
 masterNode=`echo $trinoInfo | cut -d " " -f4`
 hiveNode=`echo $hiveInfo | cut -d " " -f3`
 node=`hostname`
 
 #是否安装
 if [[ $isInstall = "true" && $installNode =~ $node ]];then
     
     #2.查看$PLAY_HOME/frames目录下是否有trino安装包
     trinoIsExists=`find $PLAY_HOME/frames -name $trino`
    
     if [[ ${#trinoIsExists} -ne 0 ]];then
           
          if [[ ! -d /opt/app ]];then
              mkdir /opt/app && chmod -R 775 /opt/app
          fi
   
          #删除旧的
          trino_home_old=`find /opt/app -maxdepth 1 -name "*trino*"`
          for i in $trino_home_old;do
                rm -rf $i
          done

          #3.解压到指定文件夹/opt/app中
          echo "开始解压trino安装包"
          tar -zxvf $trinoIsExists -C /opt/app >& /dev/null
          echo "trino安装包解压完毕"

          trino_home=`find /opt/app -maxdepth 1 -name "*trino*"`
          mkdir -p $trino_home/etc
 
          #4.配置node.properties文件
          configureNodeEnv $trino_home/etc/node.properties

          #配置jvm.config   
          configureJvm $trino_home/etc/jvm.config

          #判断在当前节点安装主节点还是从节点
          if [[ $masterNode = $node ]];then
            # 安装主节点
            configureTrinoCoordinator $trino_home/etc/config.properties $masterNode
          else
            # 安装从节点
            configureTrinoWorker $trino_home/etc/config.properties $masterNode
          fi

          #配置日志级别
          configureLogLevel $trino_home/etc/log.properties

          #配置catalog
          mkdir -p $trino_home/etc/catalog
          configureJmxCatalog $trino_home/etc/catalog/jmx.properties
          
          #安装Trino客户端
          client_home=`find $PLAY_HOME/frames/lib -maxdepth 1 -name "*trino-cli*"`
          if [[ ${#client_home} -ne 0 ]];then
              cp $client_home $trino_home/bin/trino
              chmod a+x $trino_home/bin/trino
              # trino --server localhost:8080 --catalog hive --schema default
          else
              echo "未找到trino-cli客户端，请将trino-cli-*.jar放入$PLAY_HOME/frames/lib目录"
          fi
        
          # 配置HiveCatalog
          configureHiveCatalog $trino_home/etc/catalog/hive.properties $hiveNode

          # 配置TRINO_HOME
          profile=/etc/profile
          sed -i "/^export TRINO_HOME/d" $profile
          echo "export TRINO_HOME=$trino_home" >> $profile

          # 配置PATH
          sed -i "/^export PATH=\$PATH:\$TRINO_HOME\/bin/d" $profile
          echo "export PATH=\$PATH:\$TRINO_HOME/bin" >> $profile

          # 更新/etc/profile文件
          source /etc/profile && source /etc/profile

          # 输出提示信息
          echo "--------------------"
          echo "|  Trino安装成功！ |"
          echo "--------------------"
          echo "Trino服务端口为：8080"
          
     else
         echo "$PLAY_HOME/frames目录下没有Trino安装包"
     fi
 else
     echo "Trino不允许被安装在当前节点"
 fi
}

installTrino